package fragment.listfragments;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.ListClassicEntitiy;
import fragment.AbsBaseFragment;
import tools.ItemOnClickListener;
import utils.adapter.ListClassicAdpter;
import volley.VolleySingleton;

/**
 * Created by dllo on 15/11/24.
 */
public class ListclassicFragment extends AbsBaseFragment implements ItemOnClickListener {
    private RecyclerView recyclerView;
    private ListClassicAdpter adpter;
    private TextView title;
    String titleName;
    private ImageView back;
//    String URL = "http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=android&key=ranking%3" +
//            "Aalbum%3Asubscribed%3A30%3A0&pageId=1&pageSize=40";

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_list_classic;
    }


    @Override
    protected void initView() {
        recyclerView = findView(R.id.listclassic);
        title = findView(R.id.titleForAllTv);

        back = findView(R.id.titleForAllBack);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
        adpter = new ListClassicAdpter();
        recyclerView.setAdapter(adpter);
        GridLayoutManager manager = new GridLayoutManager(getContext(),1);
        recyclerView.setLayoutManager(manager);
        adpter.setItem(this);


    }

    //静态方法传值
    public static ListclassicFragment newInstance(String URL,String titleName) {
        ListclassicFragment fragment = new ListclassicFragment();
        Bundle args = new Bundle();
        args.putString("URL", URL);
        args.putString("title", titleName);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void initData() {


        if (getArguments() != null) {
            URL = getArguments().getString("URL");
            titleName = getArguments().getString("title");
            title.setText(titleName);

        }
        initNetwork();
    }
    String URL;
    private void initNetwork() {
        VolleySingleton.getInstance().addRequest(URL,
                ListClassicEntitiy.class, new Response.Listener<ListClassicEntitiy>() {
                    @Override
                    public void onResponse(ListClassicEntitiy response) {
                        if (response != null) {

                            List<ListClassicEntitiy.ListEntity> datas = response.getList();
                            if (datas != null) {
                                adpter.addData(datas);
                            }
                            dismissDialog();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();
    }

    @Override
    public void itemListener(Object o) {
        ListClassicEntitiy.ListEntity listEntity= (ListClassicEntitiy.ListEntity)o;
        gotoNextCollect(listEntity.getAlbumId());
    }
}
